from contacts.models import Contact
from players.models import Player
from django.shortcuts import get_object_or_404, render
from django.core.paginator import EmptyPage,PageNotAnInteger,Paginator

# Create your views here.
def index(request):
    players =Player.objects.order_by('teamid')
     
    context = {
        'players': players
    }
    return render(request, 'players/players.html', context)


def player(request, player_id):
    player = get_object_or_404(Player, pk=player_id)
    user_contact = Contact.objects.order_by('-contact_date').filter(player_id=player_id)

    context = {
         'player': player,
         'contacts':user_contact
    }
    return render(request, 'players/player.html', context)


def search(request):
    queryset_list = Player.objects.order_by('teamid')
    # keywords
    if 'keywords' in request.GET:
        keywords = request.GET['keywords']
        if keywords:
            queryset_list = queryset_list.filter(teamid__icontains=keywords)
    # 
    if 'name' in request.GET:
        name = request.GET['name']
        if name:
            queryset_list = queryset_list.filter(name__iexact=name)
    #s
    if 'score' in request.GET:
        score = request.GET['score']
        if score:
            queryset_list = queryset_list.filter(score__iexact=score)
    #
    if 'time' in request.GET:
        time = request.GET['time']
        if time:
            queryset_list = queryset_list.filter(time__lte=time)
    #
    if 'percentage' in request.GET:
        percentage = request.GET['percentage']
        if percentage:
            queryset_list = queryset_list.filter(percentage__lte=percentage)
    #
    if 'shoot' in request.GET:
        shoot = request.GET['shoot']
        if shoot:
            queryset_list = queryset_list.filter(shoot__lte=shoot)
    #
    if 'hit' in request.GET:
        hit = request.GET['hit']
        if hit:
            queryset_list = queryset_list.filter(hit__lte=hit)
    #
    if 'threepercentage' in request.GET:
        threepercentage = request.GET['threepercentage']
        if threepercentage:
            queryset_list = queryset_list.filter(threepercentage__lte=threepercentage)
    #
    if 'threeshoot' in request.GET:
        threeshoot = request.GET['threeshoot']
        if threeshoot:
            queryset_list = queryset_list.filter(threeshoot__lte=threeshoot)
    #
    if 'threehit' in request.GET:
        threehit = request.GET['threehit']
        if threehit:
            queryset_list = queryset_list.filter(threehit__lte=threehit)
    #
    if 'rebound' in request.GET:
        rebound = request.GET['rebound']
        if rebound:
            queryset_list = queryset_list.filter(rebound__lte=rebound)
    #
    if 'assists' in request.GET:
        assists = request.GET['assists']
        if assists:
            queryset_list = queryset_list.filter(assists__lte=assists)
    if 'turnover' in request.GET:
        turnover = request.GET['turnover']
        if turnover:
            queryset_list = queryset_list.filter(turnover__lte=turnover)
    if 'foul' in request.GET:
        foul = request.GET['foul']
        if foul:
            queryset_list = queryset_list.filter(foul__lte=foul)
    
    

    context ={
        
    }
    return render(request, 'players/search.html', context)